﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class FunctionPageUI
    {
        private const String CACHE_KEY = "FunctionPage_GetAll";

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "FunctionPage_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetByID(int FunctionPageID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + FunctionPageID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_get", FunctionPageID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + FunctionPageID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }
        
        public DataSet GetByType(bool isAdmin)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_getbytype", isAdmin);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetChildAdminPage()
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_getChildAdminPage");
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        /// <summary>
        /// Hàm lấy về những trang không có trang con
        /// </summary>
        /// <returns></returns>
        public DataSet GetTop()
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_gettop");
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetByFunctionType(String functionType)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_getbyfunctiontype", functionType);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetRoot(String functionType)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "FunctionPage_GetRoot", functionType);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }


        public int DeleteByID(int FunctionPageID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FunctionPage_delete", FunctionPageID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int Add(String FunctionPageName, String FunctionPageAlias, String FunctionPageTitle, String FunctionPageImage, String FunctionPageIcon, String FunctionPageDescription,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, String Lang, bool IsAdmin, string FunctionType, bool IsCache, int CacheTime, String ControlPath, String URLPath)
        {
            try
            {
                if (ControlPath == null || ControlPath.Trim() == "")
                {
                    ControlPath = GetControlPath(FunctionPageAlias, FunctionType,IsAdmin);
                }
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FunctionPage_add", FunctionPageName,FunctionPageAlias,FunctionPageTitle,FunctionPageImage,FunctionPageIcon,FunctionPageDescription,
            MetaKeyword,MetaDescription,IsVisible,AllowDelete,ViewOrder,Lang,IsAdmin,FunctionType,IsCache,CacheTime,ControlPath,URLPath);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int Update(int FunctionPageID, String FunctionPageName, String FunctionPageAlias, String FunctionPageTitle, String FunctionPageImage, String FunctionPageIcon, String FunctionPageDescription,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, String Lang, bool IsAdmin, string FunctionType, bool IsCache, int CacheTime, String ControlPath, String URLPath)
        {
            try
            {
                if (ControlPath == null || ControlPath.Trim() == "")
                {
                    ControlPath = GetControlPath(FunctionPageAlias, FunctionType, IsAdmin);
                }

                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FunctionPage_update", FunctionPageID, FunctionPageName, FunctionPageAlias, FunctionPageTitle, FunctionPageImage, FunctionPageIcon, FunctionPageDescription,
            MetaKeyword, MetaDescription, IsVisible, AllowDelete, ViewOrder, Lang, IsAdmin, FunctionType, IsCache, CacheTime, ControlPath, URLPath);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + FunctionPageID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        private string GetControlPath(String functionPageAlias, String functionType, bool isAdmin)
        {
            functionPageAlias = functionPageAlias.Trim().Replace("-", "_");
            if (isAdmin)
            {
                return "/Admin/Module/" + functionPageAlias + ".ascx";
            }
            else
            {
                return "/Module/" + functionType + "/" + functionPageAlias + "_function" + ".ascx";
            }
        }
    }
}
